<template>
  <div class="comment-content">
    <h3>发表评论</h3>
    <hr>
    <textarea v-model="commentMsg" placeholder="请输入要BB 的内容！(120字以内)" maxlength="120"></textarea>
    <mt-button type="primary" size="large" @click="postComment()">发表评论</mt-button>
    <div class="comment-list">
      <div class="comment-item" v-for="(item, i) in componentList" :key="item.id">
        <div
          class="comment-title"
        >第{{i +1}}楼&nbsp;&nbsp;&nbsp;用户：{{ item.user_name }}&nbsp;&nbsp;发表时间：{{ item.add_time | dateFormat() }} &nbsp;</div>
        <div class="comment-body">{{ item.content == 'undefined' ? "此用户太懒了，啥都没写": item.content}}</div>
      </div>
    </div>
    <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      pageindex: 1,
      componentList: [],
      commentMsg: ""
    };
  },
  created() {
    this.getComments();
  },
  methods: {
    getComments() {
      //获取加载评论
      this.$http
        .get("api/getcomments/" + this.newsId + "?pageindex=" + this.pageindex)
        .then(result => {
          if (result.body.status == 0) {
            this.componentList = this.componentList.concat(result.body.message);
          }
        })
        .catch(err => {
          Toast("获取失败！");
        });
    },
    getMore() {
      //获取更多的评论
      this.pageindex++;
      this.getComments();
    },
     postComment() {
    if (this.commentMsg.trim().length == 0) {
      return Toast("请输入评论的内容！");
    }
    this.$http
      .post("api/postcomment/" + this.newsId, {
        content: this.commentMsg.trim()
      })
      .then(result => {
        if (result.body.status == 0) {
          var cms = {
            user_name: "李荣浩",
            add_time: Date.now(),
            content: this.commentMsg.trim()
          };
          this.componentList.unshift(cms);
        }
      })
      .catch(err => {});
  }
  },
  props: ["newsId"]
};
</script>
<style lang="scss" scoped>
.comment-content {
  h3 {
    font-size: 18px;
  }
  textarea {
    font-size: 14px;
    height: 85px;
    margin-bottom: 0;
  }
  .comment-list {
    margin: 5px 0;
    .comment-item {
      font-size: 13px;
      .comment-title {
        background-color: #ccc;
        text-align: center;
      }
      .comment-body {
        line-height: 35px;
        text-indent: 30px;
      }
    }
  }
}
</style>
